package com.wangjiyuan.shiwupai.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.wangjiyuan.shiwupai.R;
import com.wangjiyuan.shiwupai.base.BaseApplication;
import com.wangjiyuan.shiwupai.bean.guangchi.first.FeedsBean;
import com.wangjiyuan.shiwupai.ui.activity.photo.PhotoActivity;
import com.wangjiyuan.shiwupai.utils.RecyclerViewAdapterHelper;

import java.util.List;

/**
 * Created by wjy on 2016/11/29.
 */

public class FirstAdapter extends RecyclerViewAdapterHelper<FeedsBean> {

    public FirstAdapter(Context context, List list) {
        super(context, list);
    }

    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getContentType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        if (viewType == 5) {
            View view = mInflater.inflate(R.layout.item_first_5, parent, false);
            viewHolder = new ViewHolder_5(view);
        } else {
            View view = mInflater.inflate(R.layout.item_first_6, parent, false);
            viewHolder = new ViewHolder_6(view);
        }
        return viewHolder;
    }

    @Override
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder_5) {
            ((ViewHolder_5) holder).newInstance();
        } else if (holder instanceof ViewHolder_6) {
            ((ViewHolder_6) holder).newInstance();
        }
    }


    private class ViewHolder_5 extends RecyclerView.ViewHolder {

        private CardView item_first_5;
        private ImageView publisher_avatar;
        private TextView title, publisher, like_ct;
        private LinearLayout card_image;

        public ViewHolder_5(View itemView) {
            super(itemView);
            item_first_5 = (CardView) itemView.findViewById(R.id.item_first_5);
            ViewGroup.LayoutParams layoutParams = item_first_5.getLayoutParams();
            layoutParams.width = BaseApplication.width / 2;
            layoutParams.height = layoutParams.width * 1000 / 640;
            Log.e("aaa", layoutParams.width + "");
            card_image = (LinearLayout) itemView.findViewById(R.id.card_image);
            publisher_avatar = (ImageView) itemView.findViewById(R.id.publisher_avatar);
            title = (TextView) itemView.findViewById(R.id.title);
            publisher = (TextView) itemView.findViewById(R.id.publisher);
            like_ct = (TextView) itemView.findViewById(R.id.like_ct);
        }

        public void newInstance() {
            final FeedsBean feedsBean = mList.get(getAdapterPosition());
            Glide.with(mContext)
                    .load(feedsBean.getCardImage())
                    .asBitmap()
                    .override(BaseApplication.width >> 1, BaseApplication.width >> 1)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                            item_first_5.setBackground(new GlideBitmapDrawable(mContext.getResources(), resource));
                        }
                    });
            Glide.with(mContext)
                    .load(feedsBean.getPublisherAvatar())
                    .asBitmap()
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                            RoundedBitmapDrawable circularBitmapDrawable =
                                    RoundedBitmapDrawableFactory.create(mContext.getResources(), resource);
                            circularBitmapDrawable.setCircular(true);
                            publisher_avatar.setImageDrawable(circularBitmapDrawable);
                        }
                    });

            title.setText(feedsBean.getTitle() + "");
            publisher.setText(feedsBean.getPublisher() + "");
            like_ct.setText(feedsBean.getLikeCt() + "");

            item_first_5.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //跳转页面
                    Intent intent = new Intent(mContext, PhotoActivity.class);
                    intent.putExtra("id", feedsBean.getItemId() + "");
                    mContext.startActivity(intent);
                }
            });
        }
    }

    private class ViewHolder_6 extends RecyclerView.ViewHolder {
        private CardView item_first_6;
        private ImageView card_image;

        public ViewHolder_6(View itemView) {
            super(itemView);
            item_first_6 = (CardView) itemView.findViewById(R.id.item_first_6);
            ViewGroup.LayoutParams layoutParams = item_first_6.getLayoutParams();
            layoutParams.width = BaseApplication.width / 2;
            layoutParams.height = layoutParams.width * 410 / 330;
            card_image = (ImageView) itemView.findViewById(R.id.card_image);
        }

        public void newInstance() {
            Glide.with(mContext)
                    .load(mList.get(getAdapterPosition()).getCardImage())
                    .placeholder(R.drawable.img_home_default)
                    .centerCrop()
                    .into(card_image);
            item_first_6.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //跳转页面
                }
            });
        }
    }
}
